//
//  AssayDetialController.m
//  Nurse
//
//  Created by Sunny on 13-12-22.
//  Copyright (c) 2013年 RainSets. All rights reserved.
//

#import "AssayDetialController.h"
#import "AssayDetailCell.h"
#import "PatientInfoView.h"
#import "PatientService.h"
#import "LisService.h"

@interface AssayDetialController (){
    
}
@property(nonatomic,weak)IBOutlet UITableView *table;
@property(nonatomic,weak)IBOutlet UILabel *lblTime;
@property(nonatomic,strong) NSMutableArray* details;
@property (strong, nonatomic) IBOutlet UILabel *lblxm;

@end

@implementation AssayDetialController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.details  = [NSMutableArray array];
        [self addObserver:self forKeyPath:@"applyID" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    }
    return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context  {
    if ([keyPath isEqualToString:@"applyID"]) {
        //NSLog(@"old name:%@ \nnew name:%@", [change objectForKey:@"old"], [change objectForKey:@"new"]);
        NSString* newID = [change objectForKey:@"new"];
        __weak AssayDetialController* this = self;
        [[LisService sharedInstance]queryLisReportDetailsForID:newID
                                            withSuccessHandler:^(id result) {
                                                [this.details removeAllObjects];
                                                [this.details addObjectsFromArray:result];
                                                [this.tableView reloadData];
                                            } andFailHandler:^(NSError *error, NSString *title) {
                                                [[DataEngine sharedInstance]showMessage:[error description] withTitle:title];
                                            }];
        return;
    }
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
-(void)leftBar_clicked:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title  = @"化验报告";
    [self setleftBarButtonItem:@""orImaged:@"head-icon_04.png" Event:@selector(leftBar_clicked:)];
    
    //NSArray *ibs = [[NSBundle mainBundle] loadNibNamed:@"PatientInfoView" owner:nil options:nil];
    // TestView *test = [ibs objectAtIndex:0];
    
    NSDictionary* patient = [[PatientService sharedInstance]currentPatient];
    self.pInfoView.txtPatientName.text = [patient valueForKey:@"姓名"];
    self.pInfoView.txtPatientID.text = [NSString stringWithFormat:@"住院号:%@",[patient valueForKey:@"住院号"]];
    self.pInfoView.txtBedNo.text = [NSString stringWithFormat:@"床位号:%@",[patient valueForKey:@"床位号"]];
    NSString*xb= [patient valueForKey:@"性别"];
    if ([xb isEqualToString:@"女"]) {
        self.pInfoView.imgxb.image=[UIImage imageNamed:@"gender-female2.png"];
        
    }
    else{
        self.pInfoView.imgxb.image=[UIImage imageNamed:@"gender-male2.png"];
    }
    
    self.lblxm.text=self.Nstitle;
    self.lblTime.text=self.Nstime;
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.details count];
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
	return UITableViewCellEditingStyleNone;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *identify = @"AssayDetailCell";
    
    AssayDetailCell *cell = [tableView  dequeueReusableCellWithIdentifier:identify];
    if (!cell ) {
        NSArray *ibs = [[NSBundle mainBundle] loadNibNamed:@"AssayDetailCell" owner:self options:nil];
        cell = (AssayDetailCell *)[ibs objectAtIndex:0];
        // cell.selectionStyle = UITableViewCellSelectionStyleGray;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
    NSInteger index = [indexPath row];
    NSDictionary* record = [self.details objectAtIndex:index];
    
     cell.lbTitle.text = [record valueForKey:@"项目"];
    
    NSString* value = [NSString stringWithFormat:@"%@",[record valueForKey:@"结果"]];
    NSString* ref = [NSString stringWithFormat:@"%@",[record valueForKey:@"参考值"]];
    NSString* unit = [NSString stringWithFormat:@"%@",[record valueForKey:@"单位"]];
    
    cell.lbcode.text=[record valueForKey:@"代码"];
    cell.lbValue.text = value;
    cell.lbRef.text = ref;
    cell.lbUnit.text = unit;
    
    NSNumber* diff = [record valueForKey:@"偏差"];
    if ([diff intValue] == -1) {
        [cell setColorGreen];
    }
    else if([diff intValue] == 1){
        [cell setColorRed];
    }else{
        [cell setColorBlack];
    }
    
    //    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
    return  cell;
}

-(void)dealloc{
    [self removeObserver:self forKeyPath:@"applyID"];
}



@end
